#ifndef MAPLABEL_H
#define MAPLABEL_H

#include <QLabel>
#include "mapcache.h"
#include <QJsonObject>
#include <QNetworkAccessManager>
#include "pointcloudthread.h"


struct Corner
{
    int pos_x;
    int pos_y;
    int id_x;
    int id_y;
    Corner posXAdd(int x);
    Corner posYAdd(int y);
    Corner posXSet(int x);
    Corner posYSet(int y);
};

struct Point
{
    double x;
    double y;
    QString id;
    Point(){}
    Point(double x, double y,QString id = ""){
        this->x = x;
        this->y = y;
        this->id = id;
    }
};

struct Line
{
    Point start_point;
    Point end_point;
    int direct;
    Line(Point start, Point end, int direct){
        start_point = start;
        end_point = end;
        this->direct = direct;
    }
};

struct Curve
{
    int direct;
    QVector<Point> points;
};

class TilemapView : public QLabel
{
    Q_OBJECT
public:
    explicit TilemapView(QWidget *parent = nullptr); //设置根路径
    void init(QString host, QString port, QString map_name);
    void setRobotPose(double x, double y, double yaw, int idx);  //设置机器人位姿
    void setTrack(const QJsonArray& point_array, int div, const QJsonArray& idx_array);
    void setPointCloud(point_t *data, int size);
    void clear();
    PointCloudThread pointcloud_thread;

protected:
    virtual void mousePressEvent(QMouseEvent * event);
    virtual void mouseMoveEvent(QMouseEvent * event);
    virtual void wheelEvent(QWheelEvent * event);
    virtual bool eventFilter(QObject *obj, QEvent * event);

private:
    //http manager
    QNetworkAccessManager* manager;

    //image layer
    QLabel *layer0;
    QLabel *layer1;
    QLabel *layer2;

    Corner left_top;
    Corner right_bottom;
    double now_resolution;
    double origin_x;
    double origin_y;
    bool is_connect;
    //robot_pose
    double robot_x;
    double robot_y;
    double robot_yaw;
    int now_idx;

    int layer_map_col;
    int layer_map_row;
    int mouse_last_x;
    int mouse_last_y;
    int now_layer;
    int max_layer;
    int rate = 0;
    MapCache cache;
    QJsonObject jroot;

    QPixmap render_map;

    QPixmap robot_png;
    QPixmap point_png;
    QPixmap two_way_png;
    QPixmap forward_png;
    QPixmap backward_png;

    QVector<Point> point_set;
    QVector<Line> line_set;
    QVector<Point> track;
    QVector<int> now_idx_array;
    QVector<Point> pointcloud;

    //渲染
    void render();
    void renderPoints();
    void renderLines();
    void renderRobot();
    void renderPointcloud();

    void setLayer(int layer);

    //http slot
    void readMapJson(QNetworkReply *);
    void readConfJson(QNetworkReply *);

    //根据地图坐标算出该坐标对应的像素坐标
    double xCoordinateToPixel(double coordinate_x);
    double yCoordinateToPixel(double coordinate_y);

    //计算贝塞尔曲线
    int factorial(int num);
    QVector<Point> genBezierPointsByControlPoints(QVector<Point> contralPoints, double step);

    Point findPoint(QString point_id);
signals:

};

#endif // MAPLABEL_H
